package com.admin.action

import com.admin.service.IndexCaseAdminService
import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.bean.IndexCase
import com.data.util.ImageLookUtils
import com.data.util.JsonUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.commons.CommonsMultipartFile

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * FileName: IndexCaseAdminAction
 * Author:   HingLo
 * Date:     2017/11/6 10:02
 * Description: 首页中的案例管理
 */
@Controller
@OpenAnnotation
@NoargAnnotation
@RequestMapping("indexCase/admin/")
class IndexCaseAdminAction {
    @Autowired
    private var indexCaseAdminService: IndexCaseAdminService? = null


    @RequestMapping("list")
    fun listIndexCaseAction(request: HttpServletRequest): String {
        val list = this.indexCaseAdminService!!.listIndexCaseService()
        request.setAttribute("dataMap", list)
        return "WEB-INF/admin/index/indexCase"
    }

    /**
     * 查看图片
     *
     * @param image    图片的名称
     * @param response
     */
    @GetMapping("image")
    fun showImageAction(image: String, response: HttpServletResponse) {
        val inputStream = indexCaseAdminService!!.showImageService(image)
        //响应图片
        if (inputStream != null)
            ImageLookUtils.responseImage(inputStream, response)

    }

    /**
     * 删除展示图信息展示
     *
     * @param request
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    fun deleteIndexCaseAction(id: String, request: HttpServletRequest): Map<String, Any> {
        val bool = indexCaseAdminService!!.deleteIndexCaseService(id)
        return JsonUtils.returnMassageUtils(bool, if (bool) "删除成功" else "删除失败")
    }

    /**
     * 修改这个首页的展示图的状态，是否启用，或者禁用
     *
     * @param id
     * @param state
     * @param request
     * @return
     */
    @PostMapping("changeState")
    @ResponseBody
    fun changeStateAction(id: String, state: Boolean, request: HttpServletRequest): Map<String, Any> {

        val bool = indexCaseAdminService!!.changeStateService(id, state)
        return JsonUtils.returnMassageUtils(bool, if (bool) "操作成功" else "操作失败")
    }

    /**
     * 进入到添加banner图的界面
     *
     * @param request
     * @return
     */
    @RequestMapping("addIndex")
    fun addIndexCaseAction(request: HttpServletRequest): String = "WEB-INF/admin/index/addCase"

    /**
     * 添加banner的操作
     *
     * @param images   展示图
     * @param request
     * @return
     */
    @PostMapping("submit")
    @ResponseBody
    fun submitIndexCaseAction(@RequestParam("images") images: CommonsMultipartFile, indexCase: IndexCase, request: HttpServletRequest): Map<String, Any> {
        val bool = indexCaseAdminService!!.submitIndexCaseService(images, indexCase)
        return JsonUtils.returnMassageUtils(bool, if (bool) "添加成功" else "添加失败")
    }
}
